package zero.tech.games.log.hot;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import zero.tech.core.data.PageBean;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.entity.log.LogInviteLine;
import zero.tech.games.log.mapper.LogInviteLineMapper;
import zero.tech.redis.cache.RedisUtil;

@Component
public class LogInviteLineUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private LogInviteLineMapper logInviteLineMapper;
	@SuppressWarnings("unchecked")
	public PageBean<LogInviteLine> listInviteLineByPage(int uid, int club_id, int page, int pageLength, Date begin,
			Date end) {
		PageBean<LogInviteLine> pageBean=(PageBean<LogInviteLine>) redisUtil.hget(RedisConsts.HOT_LOG_INVITELINE_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logInviteLineMapper.totalCount(club_id,uid,begin,end);
			int start=page*pageLength;
			List<LogInviteLine> list=logInviteLineMapper.listInviteLineByPage(club_id,uid,start,pageLength,begin,end);
			pageBean = new PageBean<>(totalCount, pageLength, page, list);
			redisUtil.hset(RedisConsts.HOT_LOG_INVITELINE_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	public void insertBatch(List<LogInviteLine> logInviteLines) {
		logInviteLineMapper.insertBatch(logInviteLines);
		List<String> list=new ArrayList<String>();
		for(LogInviteLine logInviteLine:logInviteLines) {
			int club_id=logInviteLine.getClub_id();
			int uid=logInviteLine.getUid();
			String str=club_id+"_"+uid;
			if(!list.contains(str)) {
				list.add(str);
			}
		}
		for(String str:list) {
			redisUtil.del(RedisConsts.HOT_LOG_INVITELINE_PAGE+"_"+str);
		}		
	}
	@SuppressWarnings("unchecked")
	public PageBean<LogInviteLine> selectInviteLineByPage(int myuid, int club_id, int uid, int page, int pageLength,
			Date begin, Date end) {
		PageBean<LogInviteLine> pageBean=(PageBean<LogInviteLine>) redisUtil.hget(RedisConsts.HOT_LOG_INVITELINE_PAGE+"_"+club_id+"_"+myuid,page+"_"+pageLength+"_"+uid+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logInviteLineMapper.selectTotalCount(club_id,myuid,uid,begin,end);
			int start=page*pageLength;
			List<LogInviteLine> list=logInviteLineMapper.selectInviteLineByPage(club_id,myuid,uid,start,pageLength,begin,end);
			pageBean = new PageBean<>(totalCount, pageLength, page, list);
			redisUtil.hset(RedisConsts.HOT_LOG_INVITELINE_PAGE+"_"+club_id+"_"+myuid,page+"_"+pageLength+"_"+uid+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
}
